package com.yixing.tech.wcs.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.utils.HttpClientUtil;
import com.yixing.tech.common.utils.HttpResult;
import com.yixing.tech.wcs.constant.DictId;
import com.yixing.tech.wcs.domain.po.SubTask;
import com.yixing.tech.wcs.domain.po.Task;
import com.yixing.tech.wcs.domain.po.TaskCallbackInterface;
import com.yixing.tech.common.entity.WCSTaskCallbackVO;
import com.yixing.tech.wcs.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;


@Slf4j
public class DefaultTaskNotifyServiceImpl implements TaskNotifyService {

    private final ICRUDService crudService;

    public DefaultTaskNotifyServiceImpl(ICRUDService crudService) {
        this.crudService = crudService;
    }

    @Override
//    @Async
    public void notifyTaskExecResult(String subTaskCode, String action) throws BusinessException {
        SubTask subTask = crudService.querySubTaskByTaskCode(subTaskCode);
        if(ObjectUtils.isEmpty(subTask))
            return;

        Task task = crudService.queryTaskByTaskCode(subTask.getParentTask());
        if(ObjectUtils.isEmpty(task))
            return;

//        if(subTask.getStartLoc().equals(task.getStartLoc()) && "outbin".equals(action)) {
//
//        }

        TaskCallbackInterface callbackInterface = crudService.queryTaskCallbackInterfaceByBusiness(task.getBusiness());
        if(ObjectUtils.isNotEmpty(callbackInterface) && ObjectUtils.isNotEmpty(callbackInterface.getCallbackInterface())) {
            if(subTask.getEndLoc().equals(task.getEndLoc()) && "finish".equals(action)) {
                HttpResult tempRes = null;

                /** 通知离开起点 */
                WCSTaskCallbackVO callbackVO = new WCSTaskCallbackVO();
                callbackVO.setTaskCode(task.getTaskCode());
                callbackVO.setPodCode(task.getPodCode());
                callbackVO.setMethod("outbin");
                callbackVO.setCurrentPositionCode(task.getStartLoc());
                for(int i=0;i<200;++i) {
                    HttpResult result = sendRequest(callbackVO, callbackInterface.getCallbackInterface());
                    tempRes = result;
                    if(result.getStatus() != -1)
                        break;
                }
                log.info("通知上层系统任务离开起点:taskCode[{}],req[{}],res[{}]", task.getTaskCode(), callbackVO, tempRes);

                callbackVO.setMethod("finish");
                callbackVO.setCurrentPositionCode(task.getEndLoc());
                for(int i=0;i<200;++i) {
                    HttpResult result = sendRequest(callbackVO, callbackInterface.getCallbackInterface());
                    tempRes = result;
                    if(result.getStatus() != -1)
                        break;
                }

                log.info("通知上层系统任务到达终点:taskCode[{}],req[{}],res[{}]", task.getTaskCode(), callbackVO, tempRes);
            }
        }else {
            log.info("回调路径未配置:task[{}]", task);
        }
    }

    /** 通知上层业务服务发生了移库动作 */
    @Override
//    @Async
    public void notifyMovingTaskAction(String subTaskCode, String action) throws BusinessException {
        String path = "http://10.132.162.110:86/s1/wms/wms/tbea/ic/stock/out/moving/callback";

        if("finish".equals(action)) {
            SubTask subTask = crudService.querySubTaskByTaskCode(subTaskCode);
            if(ObjectUtils.isNotEmpty(subTask) && DictId.TaskType.MOVING.equals(subTask.getTaskType())) {

                Task task = crudService.queryTaskByTaskCode(subTask.getParentTask());
                if(ObjectUtils.isNotEmpty(task)) {
                    if(!subTask.getStartLoc().equals(task.getStartLoc()) && !subTask.getEndLoc().equals(task.getEndLoc())) {
                        log.info("子任务为正常出入库中产生的移库任务，上报给业务服务:subTaskCode[{}],taskCode[{}]", subTask.getTaskCode(), task.getTaskCode());

                        WCSTaskCallbackVO.MovingTaskCallbackVO callbackVO = new WCSTaskCallbackVO.MovingTaskCallbackVO();
                        callbackVO.setStartLoc(subTask.getStartLoc());
                        callbackVO.setEndLoc(subTask.getEndLoc());
                        callbackVO.setPodCode(subTask.getPodCode());
                        for(int i=0;i<5;++i) {
                            HttpResult result = sendRequest(callbackVO, path);
                            if(result.getStatus() != -1)
                                break;
                        }
                    }
                }
            }
        }
    }

    private static HttpResult sendRequest(WCSTaskCallbackVO callbackVO, String url) {
        String jsonData = JSONObject.toJSONString(callbackVO);
        return HttpClientUtil.getInstance().postJson(url, jsonData);
    }

    private static HttpResult sendRequest(WCSTaskCallbackVO.MovingTaskCallbackVO callbackVO, String url) {
        String jsonData = JSONObject.toJSONString(callbackVO);
        return HttpClientUtil.getInstance().postJson(url, jsonData);
    }

}
